package com.zzw.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zzw.bean.ClassOfStudent;
import com.zzw.bean.Student;
import com.zzw.bean.User;
import com.zzw.service.imple.IClassOfStudentService;
import com.zzw.service.imple.IStudentService;
import com.zzw.utils.*;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:spring-mybatis.xml"})
@Controller
public class StudentController {
	
	@Autowired
	IStudentService iStudentService;
	@Autowired
	IClassOfStudentService iClass;
	
	/**
	 * 转发页面
	 * */
	@RequestMapping("/studentList.do")
	@ResponseBody
	public ModelAndView studentList(Student student) {
		ModelAndView mv=new ModelAndView();
		List<Student> list=iStudentService.selectStudent(student);
		List<ClassOfStudent> classNameList=iClass.selectClass(null);
		
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("list", list);
		map.put("classNameList",classNameList);
		
		mv.addAllObjects(map);
		mv.setViewName("student_list");
		return mv;
	}
	
	/**
	 * 添加用户
	 * */
	@RequestMapping("addStudent.do")
	@ResponseBody
	public Result addStudent(Student student) {
		Result result=null;
		iStudentService.addStudent(student);
		result=ResultUtils.success();
		return result;
	}
	

	
	/**
	 * 删除用户
	 * */
	@RequestMapping("deleteStudent.do")
	@ResponseBody
	public Result deleteStudent(Integer s_id) {
		Result result=null;
		iStudentService.deleteStudent(s_id);
		result=ResultUtils.success();
		return result;
	}
	
	/**
	 * id查询单个用于用户
	 * */
	@RequestMapping("/selectStudentById.do")
	@ResponseBody
	public Result selectStudentById(Integer id) {
		Result result=null;
		Student student=iStudentService.selectStudentById(id);
		result=ResultUtils.success(student);
		return result;
	}
	
	/**
	 * 更新单个用于用户
	 * */
	@RequestMapping("/updateStudent.do")
	@ResponseBody
	public Result updateStudent(Student student) {
		Result result=null;
		iStudentService.updateStudent(student);
		result=ResultUtils.success();
		return result;
	}

}
